#include "geometry.hpp"
#include <cmath>
#include <vector>

// 计算多边形面积
double calculatePolygonArea(const std::vector<Point>& points) {
    double area = 0.0;
    size_t j = points.size() - 1;
    
    for (size_t i = 0; i < points.size(); i++) {
        area += (points[j].x + points[i].x) * (points[j].y - points[i].y);
        j = i;
    }
    
    return std::abs(area / 2.0);
}

// 计算所有多边形的面积
void calculateAllPolygonAreas(Dataset& dataset) {
    for (auto& polygon : dataset.polygons) {
        std::vector<Point> polygonPoints;
        polygonPoints.reserve(polygon.pointIndices.size());
        
        // 收集多边形的所有点
        for (int index : polygon.pointIndices) {
            polygonPoints.push_back(dataset.allPoints[index]);
        }
        
        polygon.area = calculatePolygonArea(polygonPoints);
    }
} 